import axios from 'axios'
// 配置请求的跟路径
axios.defaults.baseURL = 'http://127.0.0.1:8888/api/private/v1/';

axios.interceptors.request.use(config => {
  // console.log(config)
  config.headers.Authorization = window.sessionStorage.getItem('token')
  // 在最后必须 return config
  return config
})

//添加响应拦截器
axios.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  return response;
}, function (error) {
  // 对响应错误做点什么
  return Promise.reject(error);
});

export const login_api = (params => {
  return axios.post("login", params);
})

export const getMenuList_api = (params => {
  return axios.get("menus", params);
})

export const getUserList_api = (params => {
  return axios.get("users", params);
})

/* 用户状态管理 */
export const userStateChanged_api = (params => {
  return axios.put(`users/${params.uId}/state/${params.type}`);
})

/* 添加用户 */
export const addUsers_api = (params => {
  return axios.post(`users`, params);
})

/* 编辑用户信息  根据 ID 查询用户信息 */
export const getUsersId_api = (params => {
  return axios.get(`users/${params}`);
})

/* 编辑用户提交 */
export const editUserInfo_api = (params => {
  return axios.put(`users/${params.id}`, { email: params.email, mobile: params.mobile });
})

/* 删除单个用户 */
export const removeUserById_api = (params => {
  return axios.delete(`users/${params}`);
})

/* 权限 */
/* 权限列表  type=list*/
export const getRightsList_api = (() => {
  return axios.get(`rights/list`);
})
/* 角色列表 */
export const getRolesList_api = (() => {
  return axios.get(`roles`);
})
/* 根据Id删除对应的权限 */
export const removeRightById_api = ((params) => {
  return axios.delete(`roles/${params.roleId}/rights/${params.rightId}`);
})

/* 权限列表  type=tree*/
export const getRightsTree_api = (() => {
  return axios.get(`rights/tree`);
})

/* 角色授权  type=tree*/
export const allotRights_api = ((params) => {
  return axios.post(`roles/${params.roleId}/rights`, { rids: params.rids });
})

/* 角色列表 */
// export const getRolesList_api = ((params) => {
//   return axios.get(`roles`);
// })


/* 分配用户角色 */
export const saveRoleInfo_api = ((params) => {
  return axios.put(`users/${params.id}/role`, { rid: params.rid });
})

/* 商品分类列表 */
export const getCateList_api = ((params) => {
  return axios.get(`categories`, {
    params: params
  });
})
/* 添加分类 */
export const addCate_api = ((params) => {
  return axios.post(`categories`, params);
})

/* 分类的Id，和当前所处的面板，获取对应的参数 */
export const paramsId_api = ((params) => {
  return axios.get(`categories/${params.cateId}/attributes`, {
    params: { sel: params.sel }
  });
})

/* 添加参数 */
export const addParams_api = ((params) => {
  return axios.post(`categories/${params.cateId}/attributes`,
    { attr_name: params.attr_name, attr_sel: params.attr_sel }
  );
})
/* 修改参数 */
export const editParams_api = ((params) => {
  return axios.put(`categories/${params.cateId}/attributes/${params.attrId}`,
    { attr_name: params.attr_name, attr_sel: params.attr_sel }
  );
})

/* 根据 ID 查询参数 */
export const editParamsId_api = ((params) => {
  return axios.get(`categories/${params.cateId}/attributes/${params.attrId}`,
    {
      params: { attr_sel: params.attr_sel }
    }
  );
})

/*编辑提交参数 */
export const editParamsPost_api = ((params) => {
  return axios.put(`categories/${params.cateId}/attributes/${params.attrId}`,
  { attr_name: params.attr_name, attr_sel: params.attr_sel }
  );
})



export  const  removeParams_api=(params)=>{
  return  axios.delete(`categories/${params.id}/attributes/${params.attrid}`)
}

// 添加参数到数据库

export  const  saveAttrVals_api=(params)=>{
  return  axios.put(`categories/${params.cateId}/attributes/${params.attr_id}`,{
      attr_name: params.attr_name,
      attr_sel: params.attr_sel,
      attr_vals: params.attr_vals
    
  })
}

//商品列表
export  const  getGoodsList_api=(params)=>{
  return  axios.get(`goods`,{
    params:params
  })
}
//商品删除

export  const  removeById_api=(id)=>{
  return  axios.delete(`goods/${id}`);
}

// 商品添加
export  const  addGoods_api=(params)=>{
  return  axios.post(`goods`,params);
}
